﻿using Exceptionless.Models.Data;
using LDM.Admin.Models.Login;
using LDM.Framework.NormalizeActionResult;
using LDM.Sys.ServiceAPI.Model.DBModel;
using LDM.Sys.ServiceAPI.Model.ViewModel.Request;
using LDM.Sys.ServiceAPI.Model.ViewModel.Response.Menu;
using LDM.Sys.ServiceAPI.Svc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace LDM.Admin.Controllers
{
    public class MenuController : Controller
    {
        private readonly IMenuService _menuService;

        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取用户菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult UserMenu()
        {
            List<UserMenuViewRes> userMenu = _menuService.GetCurrentUserMenu().Result;
            return Json(ResultData.Success(userMenu));
        }



        /// <summary>
        /// 获取菜单集合
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetMenus()
        {
            List<SysMenu> userMenu = await _menuService.GetSysMenus();
            return Ok(userMenu);
        }


        /// <summary>
        /// 获取菜单集合
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ChildMenus(int MenuId)
        {
            List<SysMenu> result = new();
            var userMenus = await _menuService.GetSysMenus();
            var userMenu = userMenus.FirstOrDefault(x => x.Id == MenuId);
            if (userMenu != null)
            {
                result = userMenu.ChildMenu;
            }
            return Ok(result);
        }
    }
}
